﻿var start = new Date();
var curtab = "";
var firstShowMM = true;
var setting;
var tree = null;
var zNodes = [];
var lastTabs = new Array();
function refreshTree(asyncUrl) {
    setting = {
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId"
            }
        },
        checkable: false,
        open: true,
        async: {
            enable: true,
            url: asyncUrl,
            autoParam: ["name", "id"],
            type: "post"
        },
        callback: {
            onClick: zTreeOnClick,
            onAsyncSuccess: zTreeOnAsyncSuccess
        }
    };
    tree = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
}

function zTreeOnAsyncSuccess() {
    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
}

// 延时
function delay(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime) {
            return;
        }
    }
}

function onRightClick(event, treeId, treeNode) {
    $('#addNode').attr('disabled', true);
    $('#delNode').attr('disabled', true);
    if (treeNode && !treeNode.noR) { // 判断点击的是tree节点
        tree.selectNode(treeNode); // 选中tree节点
        if (!treeNode.isParent) {
            if (treeNode.action_url != "") {
            }
        }
    }
    // 在ztree右击事件中注册easyui菜单的显示和点击事件，让这两个框架实现共用event，这个是整合的关键点
    $('#mm1').menu(
        {
            onClick: function (item) {
                var nodes = tree.getSelectedNodes();
                var node = nodes[0];
                if (item.id == 'addNode') {
                    addFunction(node);// alert("新增节点");
                } else if (item.id == 'delNode'
                    && !$('#delNode').attr('disabled')) {
                    delMenu(node);
                } else if (item.id == 'refNode') {
                    refreshTree(path + '/system/register/iniMenu');
                }
            }
        });
    $('#mm1').menu('show', {
        left: event.pageX,
        top: event.pageY
    });
}

function zTreeOnClick(event, treeId, treeNode) {
    var now = new Date();
    if (now.getTime() - start.getTime() > 600) {
        if (!treeNode.isParent) {
            if (treeNode.menu_type != "0") {
                if (treeNode.action_url == "") {

                } else {
                    addTab(treeNode.name, treeNode.action_url, "", treeNode.id);
                    start = now;
                }
            } else {
                openUrl(treeNode.action_url);
            }
        }
    }
}

$(function () {
    $('#tabs').tabs({
        onSelect: function (tt) {
            // 移除 tt
            lastTabs = $.grep(lastTabs, function (n, i) {
                return n != tt;
            });
            // 重新压入，保证 最新的在最上面
            lastTabs.push(tt);
            // 更新当前
            currentTabTitle = tt;
        },
        onClose: function (tt) {
            // 移除 tt
            lastTabs = $.grep(lastTabs, function (n, i) {
                return n != tt;
            });
            // 重新选择
            $('#tabs').tabs('select', lastTabs[lastTabs.length - 1]);
        }
    });

    tabClose();
    tabCloseEven();
    var currTab = $('#tabs').tabs('getSelected');

    $('#tabs').tabs('update', {
        tab: currTab,
        options: {
            content: createFrame('welcome', path + '/system/register/sysWelcome')
        }
    })

    $('#mm').menu({
        onShow: function (pos) {
            if (firstShowMM) {
                var mask_mm = $("#mm").next();
                var dom_obj = $('#iframebar').clone();
                dom_obj.width(mask_mm.width() - 2);
                dom_obj.height(mask_mm.height() - 2);
                $('#iframebar').remove();
                mask_mm.append(dom_obj);
                firstShowMM = false;
            }
        }
    });
})

// 获取左侧导航的图标
function getIcon(menuId) {
    var icon = 'icon ';
    $.each(_menus.menus, function (i, n) {
        $.each(n.menus, function (j, o) {
            if (o.menuId == menuId) {
                icon += o.icon;
            }
        })
    })
    return icon;
}

function openUrl(url) {
    window
        .showModalDialog(
            path + url,
            "",
            "dialogwidth:"
            + width
            + ";dialogheight:"
            + height
            + ";help=no;status=no;center=yes;edge=sunken;resizable=yes;Minimize=no;Maximize=no");
}

function addTab(subtitle, url, icon, id) {
    var isExist = $("#tabs").tabs("existsById", id);
    if (isExist) {
        var tab = $("#tabs").tabs("selectById", id);
    } else {
        //设置打开业务模块id
        setGlobalVariable("menuId",id);

        $('#tabs').tabs('add', {
            id: id,
            title: subtitle,
            content: createFrame(id, path + url),
            closable: true,
            icon: icon
        });
    }
    curtab = subtitle;
    tabClose();
}

//关闭当前tab
function closeCurrentTab() {
    var tab = $('#tabs').tabs('getSelected');
    var subtitle = $('.tabs-selected').text();
    $('#tabs').tabs('close', subtitle);
}

function createFrame(id, url) {
    var s = '<iframe name="tab-frame' + id + '" id="' + id + '" scrolling="auto" frameborder="0"  src='
        + url + ' " style="width:100%;height:100%;"></iframe>';
    return s;
}

function tabClose() {
    /* 双击关闭TAB选项卡 */
    $(".tabs-inner").dblclick(function () {
        var subtitle = $(this).children(".tabs-closable").text();
        $('#tabs').tabs('close', subtitle);
    });
    /* 为选项卡绑定右键 */
    $(".tabs-inner").bind('contextmenu', function (e) {
        $('#mm').menu('show', {
            left: e.pageX,
            top: e.pageY
        });
        var subtitle = $(this).children(".tabs-closable").text();
        $('#mm').data("currtab", subtitle);
        $('#tabs').tabs('select', subtitle);
        return false;
    });
}
// 绑定右键菜单事件
function tabCloseEven() {
    // 刷新
    $('#mm-tabupdate').click(function () {
        var currTab = $('#tabs').tabs('getSelected');
        var url = $(currTab.panel('options').content).attr('src');
        var id = $(currTab.panel('options').content).attr('id');
        //设置打开业务模块id
        setGlobalVariable("menuId",id);
        $('#tabs').tabs('update', {
            tab: currTab,
            options: {
                content: createFrame(id, url)
            }
        })
    })
    // 关闭当前
    $('#mm-tabclose').click(function () {
        var tab = $('#tabs').tabs('getSelected');
        var index = $('#tabs').tabs('getTabIndex', tab);
        $('#tabs').tabs('close', index);
    })
    // 全部关闭
    $('#mm-tabcloseall').click(function () {
        $('.tabs-inner span').each(function (i, n) {
            var t = $(n).text();
            if (t != "我的桌面")
                $('#tabs').tabs('close', t);
        });
    });
    // 关闭除当前之外的TAB
    $('#mm-tabcloseother').click(function () {
        $('#mm-tabcloseright').click();
        $('#mm-tabcloseleft').click();
    });
    // 关闭当前右侧的TAB
    $('#mm-tabcloseright').click(function () {
        var nextall = $('.tabs-selected').nextAll();
        if (nextall.length == 0) {
            return false;
        }
        nextall.each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            if (t != "我的桌面")
                $('#tabs').tabs('close', t);
        });
        return false;
    });
    // 关闭当前左侧的TAB
    $('#mm-tabcloseleft').click(function () {
        var prevall = $('.tabs-selected').prevAll();
        if (prevall.length == 0) {
            return false;
        }
        prevall.each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            if (t != "我的桌面")
                $('#tabs').tabs('close', t);
        });
        return false;
    });

    // 退出
    $("#mm-exit").click(function () {
        $('#mm').menu('hide');
    })
}

/**
 * 关闭所有tab页
 */
function closeAllTabs() {
    $('.tabs-inner span').each(function (i, n) {
        var t = $(n).text();
        if (t != "我的桌面") {
            $('#tabs').tabs('close', t);
        }
    });
}

//刷新当前标签Tabs
function RefreshTab(currentTab) {
    var url = $(currentTab.panel('options')).attr('href');
    $('#tabs').tabs('update', {
        tab: currentTab,
        options: {
            href: url
        }
    });
    currentTab.panel('refresh');
}

